<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>2. 改善应用配置 | 被删的前端游乐场</title>
    <meta name="description" content="Just playing around">
    
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.a683ed26.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.e5cf4092.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.c853767c.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.eed49fdd.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/54.6cc65510.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.a683ed26.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-0cd75e06><span class="stand" data-v-0cd75e06></span> <div class="cat" data-v-0cd75e06><div class="body" data-v-0cd75e06></div> <div class="head" data-v-0cd75e06><div class="ear" data-v-0cd75e06></div> <div class="ear" data-v-0cd75e06></div></div> <div class="face" data-v-0cd75e06><div class="nose" data-v-0cd75e06></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div></div> <div class="tail-container" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>玩转 Angular</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/angular-free/0-about-angular-from-1-to-4.html" class="sidebar-link">谈谈 AngularJS 到 Angular</a></li><li><a href="/front-end-playground/front-end-others/angular-free/0-di-in-angular.html" class="sidebar-link">谈谈 Angular 的依赖注入</a></li><li><a href="/front-end-playground/front-end-others/angular-free/1-init-angular-app-with-webpack.html" class="sidebar-link">1. 用 Webpack 启动 Angular 应用</a></li><li><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html" class="active sidebar-link">2. 改善应用配置</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html#component-中加载-html-css" class="sidebar-link">Component 中加载 html/css</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html#使用-angular2-template-loader" class="sidebar-link">使用 angular2-template-loader</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html#结束语" class="sidebar-link">结束语</a></li></ul></li><li><a href="/front-end-playground/front-end-others/angular-free/3-init-routes-and-assets.html" class="sidebar-link">3. 启用路由和添加静态资源</a></li><li><a href="/front-end-playground/front-end-others/angular-free/4-create-sidebar.html" class="sidebar-link">4. 制作左侧自动定位菜单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/5-custom-input-component.html" class="sidebar-link">5. 自定义 input 表单控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/6-two-form-building.html" class="sidebar-link">6. 模型驱动和模板驱动的表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/7-dynamic-form.html" class="sidebar-link">7. 创建动态表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/8-radio-and-checkbox.html" class="sidebar-link">8. 表单的 radio 和 checkbox</a></li><li><a href="/front-end-playground/front-end-others/angular-free/9-upload-iamge.html" class="sidebar-link">9. 图片上传控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html" class="sidebar-link">10. 向表单添加条件控制</a></li><li><a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html" class="sidebar-link">11. 使用动态表单制作选项配置对话框</a></li><li><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html" class="sidebar-link">12. 配置以及生成表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/13-dynamic-list.html" class="sidebar-link">13. 动态列表配置</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>BOX2D.js 文档与教程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>该系列用于探索一些 Angular 中灵活或者新的用法。本文记录改善应用中一些 html 和 css 加载的问题。</p> <h2 id="component-中加载-html-css"><a href="#component-中加载-html-css" aria-hidden="true" class="header-anchor">#</a> Component 中加载 html/css</h2> <h3 id="使用-file-loader"><a href="#使用-file-loader" aria-hidden="true" class="header-anchor">#</a> 使用 file-loader</h3> <p>上一节我们使用简单粗暴的<code>file-loader</code>生成文件路径方式进行模板加载，我们的组件定义的代码长这样：</p> <div class="language-js extra-class"><pre class="language-js"><code>@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token punctuation">:</span> <span class="token string">&quot;app-root&quot;</span><span class="token punctuation">,</span>
  templateUrl<span class="token punctuation">:</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;file-loader?name=[path][name].[ext]!./app.component.html&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">AppComponent</span> <span class="token punctuation">{</span>
  title <span class="token operator">=</span> <span class="token string">&quot;app works!&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>这样做会有一些问题：</p> <ol><li>使用的时候很不方便，要写一长串。</li></ol> <p>该方法可以通过在 webpack 里对 file-loader 进行配置解决，但可能会产生一些其他问题。本骚年折腾了下没折腾好，估计是姿势不正确吧，大家可以下来试试。</p> <ol start="2"><li>生成的代码不打包进<code>bundle.js</code>文件。</li></ol> <p>这样有个很明显的问题，就是生成的代码体积和大小都比较浪费资源。除非是像本骚年之前的项目一样需要远程 debug 和直接线上修 bug，一般来说都不会需要这样处理的。</p> <h3 id="其他方式"><a href="#其他方式" aria-hidden="true" class="header-anchor">#</a> 其他方式</h3> <p>当然，像 angular-cli 以及 Angular2 Webpack Starter 这些脚手架和项目，已经对 loader 进行了调整，最终我们是感受不到对 html/css 等的文件加载，同时也能很方便地通过 sourcemap 定位 bug。</p> <p>这里要不使用以上脚手架的话，就得自己想办法实现这个过程了，当然有能力的话自己写个 loader 也完全没问题，可惜目前本骚年对 npm 和 node 的掌握也很是局限(T_T)。实话说，个人觉得使用一个<code>file-loader</code>其实说不定也能解决这样的问题了。</p> <p>下面是从<a href="https://github.com/AngularClass/angular2-webpack-starter" target="_blank" rel="noopener noreferrer">Angular2 Webpack Starter<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>里面翻到的，其中使用的<code>angular2-template-loader</code>的使用过程。</p> <h2 id="使用-angular2-template-loader"><a href="#使用-angular2-template-loader" aria-hidden="true" class="header-anchor">#</a> 使用 angular2-template-loader</h2> <h3 id="angular2-template-loader"><a href="#angular2-template-loader" aria-hidden="true" class="header-anchor">#</a> angular2-template-loader</h3> <p>关于安装和使用等说明，都可以参考<a href="https://github.com/TheLarkInn/angular2-template-loader" target="_blank" rel="noopener noreferrer">angular2-template-loader 文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p> <p>这里我们不再需要<code>file-loader</code>了，同时我们需要添加<code>angular2-template-loader</code>以及<code>raw-loader</code>。</p> <p>其中<code>raw-loader</code>常用于把文件内容作为字符串返回。</p> <h3 id="package-json"><a href="#package-json" aria-hidden="true" class="header-anchor">#</a> package.json</h3> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;angular-free&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;0.0.0&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;build&quot;</span><span class="token operator">:</span> <span class="token string">&quot;webpack --config webpack.config.js&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;dev&quot;</span><span class="token operator">:</span> <span class="token string">&quot;webpack-dev-server --config webpackServer.config.js --host 0.0.0.0 --port 3333 --devtool eval --progress --colors --hot --content-base dist&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;dependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;@angular/common&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/compiler&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/compiler-cli&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/core&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/forms&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/http&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/platform-browser&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/platform-browser-dynamic&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;@angular/router&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~4.1.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;rxjs&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^5.1.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;zone.js&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^0.8.10&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;devDependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;@types/node&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^7.0.18&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;angular2-template-loader&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~0.6.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;babel-core&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~6.24.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;babel-loader&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~7.0.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;babel-polyfill&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~6.23.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;babel-preset-latest&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^6.24.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;html-webpack-plugin&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^2.28.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;raw-loader&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^0.5.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;ts-loader&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^2.0.3&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;tslint&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^5.2.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;typescript&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~2.2.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;webpack&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~2.5.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;webpack-dev-server&quot;</span><span class="token operator">:</span> <span class="token string">&quot;~2.4.5&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>这里本骚年也调整了一下依赖对应的版本，毕竟像 angular 这样的不兼容升级方式，还是做点记号的好。</p> <h3 id="webpack-config-js"><a href="#webpack-config-js" aria-hidden="true" class="header-anchor">#</a> webpack.config.js</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 这里调整了loader</span>
rules<span class="token punctuation">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        test<span class="token punctuation">:</span> <span class="token regex">/\.ts$/</span><span class="token punctuation">,</span>
        use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;babel-loader&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;ts-loader&quot;</span><span class="token punctuation">,</span> <span class="token string">'angular2-template-loader'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        exclude<span class="token punctuation">:</span> <span class="token regex">/node_modules/</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        test<span class="token punctuation">:</span> <span class="token regex">/\.(html|css)$/</span><span class="token punctuation">,</span>
        use<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'raw-loader'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        exclude<span class="token punctuation">:</span> <span class="token punctuation">[</span>path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'src/index.html'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        test<span class="token punctuation">:</span> <span class="token regex">/\.async\.(html|css)$/</span><span class="token punctuation">,</span>
        loaders<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'file?name=[name].[ext]'</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token operator">...</span>
<span class="token punctuation">]</span>
</code></pre></div><h3 id="app-component-ts"><a href="#app-component-ts" aria-hidden="true" class="header-anchor">#</a> app.component.ts</h3> <div class="language-js extra-class"><pre class="language-js"><code>@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token punctuation">:</span> <span class="token string">&quot;app-root&quot;</span><span class="token punctuation">,</span>
  templateUrl<span class="token punctuation">:</span> <span class="token string">&quot;./app.component.html&quot;</span><span class="token punctuation">,</span>
  styleUrls<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;app.style.css&quot;</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">AppComponent</span> <span class="token punctuation">{</span>
  title <span class="token operator">=</span> <span class="token string">&quot;app works!&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>最终使用的方式如上，与<code>angular2-template-loader</code>官方推荐的<code>require</code>方法不一致，但是这样更加方便了。</p> <h2 id="结束语"><a href="#结束语" aria-hidden="true" class="header-anchor">#</a> 结束语</h2> <p>这节主要讲了使用<code>angular2-template-loader</code>替换之前的<code>file-loader</code>，来改善 angular2 中 html 文件的引入方式。<br> <a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-free/2-inprove-app-configuration" target="_blank" rel="noopener noreferrer">此处查看项目代码<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a><br> <a href="http://angular2-free.godbasin.com/2-inprove-app-configuration/index.html" target="_blank" rel="noopener noreferrer">此处查看页面效果<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/angular-free/2-inprove-app-configuration.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/angular-free/1-init-angular-app-with-webpack.html" class="prev">
          1. 用 Webpack 启动 Angular 应用
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/angular-free/3-init-routes-and-assets.html">
          3. 启用路由和添加静态资源
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-3275" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty2.svg);"></div></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.e5cf4092.js" defer></script><script src="/front-end-playground/assets/js/3.c853767c.js" defer></script><script src="/front-end-playground/assets/js/2.eed49fdd.js" defer></script><script src="/front-end-playground/assets/js/54.6cc65510.js" defer></script>
  </body>
</html>
